﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RPSImporter.Infrastructure;
using RPSImporter.Services.Data;
using RPSImporter.Infrastructure.Attributes;
using RPSImporter.Models.Settings;
using RPSImporter.Services;

namespace RPSImporter.Models
{
   [FileTypeName("EnterpriseExtern_FileType")]
   public class EnterpriseExt : ModelBase, IModel
   {
      public EnterpriseExt()
      {
         this.FileSettings = DatabaseSvc.Instance.Settings.EnterpriseFileSettings.First();
      }

      public EnterpriseFileSettings FileSettings { get; set; }

      public bool Import()
      {
         try
         {
            if (this.GetData(this.FileSettings.Name) != String.Empty
               && !this.DB.ENTERPRISEs.Any(e => e.NAME == this.GetData(this.FileSettings.Name)))
            {
               ENTERPRISE enterprise = new ENTERPRISE();
               enterprise.NAME = this.GetData(this.FileSettings.Name);
               enterprise.ADDRESS1 = this.GetData(this.FileSettings.Address1);
               enterprise.ADDRESS2 = this.GetData(this.FileSettings.Address2);
               enterprise.CITY = this.DB.CITies.Where(c => c.NAME == this.GetData(this.FileSettings.City)).FirstOrDefault();
               enterprise.POSTAL_CODE = this.GetData(this.FileSettings.PostalCode);
               enterprise.POSTAL_BOX = this.GetData(this.FileSettings.PostalBox);
               enterprise.ID_COUNTRY = this.GetData(this.FileSettings.Country) != null ?
                                          this.DB.COUNTRies.Where(c => c.NAME == this.GetData(this.FileSettings.Country)).Select(c => c.ID).FirstOrDefault() : (long?)null;
               enterprise.PHONE = this.GetData(this.FileSettings.Phone);
               enterprise.FAX = this.GetData(this.FileSettings.Fax);
               enterprise.EMAIL = this.GetData(this.FileSettings.Email);
               enterprise.CONTACT_PERSON = this.GetData(this.FileSettings.ContactPerson);
               enterprise.ActivitySector = this.DB.ActivitySectors.Where(a => a.Name == this.GetData(this.FileSettings.ActivitySector)).FirstOrDefault();
               enterprise.BE_N__D_ENTREPRISE__N__D_ENTREPRISE = this.GetData(this.FileSettings.NoSiret);
               enterprise.BE_CODE_RISQUE_ENTREPRISE = this.GetData(this.FileSettings.CodeRisque);
               enterprise.FR_CODE_APE = this.GetData(this.FileSettings.CodeAPE);
               enterprise.EnterpriseType = 1;

               this.DB.ENTERPRISEs.InsertOnSubmit(enterprise);
               this.DB.SubmitChanges();
            }
            else
            {
               ENTERPRISE enterprise = this.DB.ENTERPRISEs.FirstOrDefault(e => e.NAME == this.GetData(this.FileSettings.Name));
               enterprise.NAME = this.GetData(this.FileSettings.Name);
               enterprise.ADDRESS1 = this.GetData(this.FileSettings.Address1);
               enterprise.ADDRESS2 = this.GetData(this.FileSettings.Address2);
               enterprise.CITY = this.DB.CITies.Where(c => c.NAME == this.GetData(this.FileSettings.City)).FirstOrDefault();
               enterprise.POSTAL_CODE = this.GetData(this.FileSettings.PostalCode);
               enterprise.POSTAL_BOX = this.GetData(this.FileSettings.PostalBox);
               enterprise.ID_COUNTRY = this.GetData(this.FileSettings.Country) != null ?
                                          this.DB.COUNTRies.Where(c => c.NAME == this.GetData(this.FileSettings.Country)).Select(c => c.ID).FirstOrDefault() : (long?)null;
               enterprise.PHONE = this.GetData(this.FileSettings.Phone);
               enterprise.FAX = this.GetData(this.FileSettings.Fax);
               enterprise.EMAIL = this.GetData(this.FileSettings.Email);
               enterprise.CONTACT_PERSON = this.GetData(this.FileSettings.ContactPerson);
               enterprise.ActivitySector = this.DB.ActivitySectors.Where(a => a.Name == this.GetData(this.FileSettings.ActivitySector)).FirstOrDefault();
               enterprise.BE_N__D_ENTREPRISE__N__D_ENTREPRISE = this.GetData(this.FileSettings.NoSiret);
               enterprise.BE_CODE_RISQUE_ENTREPRISE = this.GetData(this.FileSettings.CodeRisque);
               enterprise.FR_CODE_APE = this.GetData(this.FileSettings.CodeAPE);
               enterprise.EnterpriseType = 1;
               this.DB.SubmitChanges();
            }
            return true;
         }
         catch (Exception ex)
         {
            this.Error = ex.Message;
            return false;
         }
      }
   }
}